import * as PIXI from 'pixi.js';
import StageConf from '../../conf/StageConf';
import ScalerUtil from '../../util/ScalerUtil';

export default class GridLayer extends PIXI.Sprite{
    view:PIXI.Graphics;
    points:Array<any>;
    constructor(){
        super();
        this.view = new PIXI.Graphics();
        this.view.interactive = true;
        this.view.on("pointerdown", this.onAdd);

        this.points = [];
        this.addChild(this.view);
        this.init();
    }

    init(){
        this.draw();
    }

    onAdd(e:PIXI.InteractionEvent){
        var obj = e.data.getLocalPosition(this);
        console.log(obj);
    }

    addPoint(x:number, y:number):void{
        this.points.push({
            x, y
        })
    }

    draw(){
        this.view.clear();
        // this.view.beginFill(0xffffff, 1);
        // this.view.drawRect(300, 300, 3000, 3000);
        // this.view.endFill();
        
        var {width, height, scale, grid_space} = StageConf;
        var space = grid_space;
        // this.view.scale.set(scale, scale);

        var col = Math.ceil(width / space);
        var row = Math.ceil(height / space);

        var weight = ScalerUtil.pxSize;
        this.view.lineStyle(weight, 0, 0.04);
        for(let i = 0; i < col; i++){
            this.view.moveTo(space * i, 0);
            this.view.lineTo(space * i, height);
        }
        for(let i = 0; i < row; i++){
            this.view.moveTo(0, space * i);
            this.view.lineTo(width, space * i);
        }
        this.view.lineStyle(weight, 0x0000ff, 0.12);
        this.view.moveTo(width / 2, 0);
        this.view.lineTo(width / 2, height);
        this.view.moveTo(0, height / 2);
        this.view.lineTo(width, height / 2);

        this.view.beginFill(0xff0000, 0.3);
        for(let i = 0; i < this.points.length; i++){
            this.view.drawRect(this.points[i].x, this.points[i].y, space, space);
        }
    }
    
}